package com.cyh.mylib.utils;

import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;

/**
 * @author 程延宏
 * @description:SpannableString 工具类
 * @date :9/2 15:31
 */
public class SpannableStringUtils {

    //设置Url地址连接
    public static  CharSequence addUrlSpan(CharSequence charSequence,int start ,int end) {
        SpannableString spanString = new SpannableString(charSequence);
        URLSpan span = new URLSpan(charSequence.toString());
        spanString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanString;
    }

    //设置字体背景的颜色
    public static  CharSequence addBackColorSpan(CharSequence charSequence,int start ,int end) {
        SpannableString spanString = new SpannableString(charSequence);
        BackgroundColorSpan span = new BackgroundColorSpan(Color.YELLOW);
        spanString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanString;
    }

    //设置字体的颜色
    public static  CharSequence addForeColorSpan(CharSequence charSequence,int start ,int end) {
        SpannableString spanString = new SpannableString(charSequence);
        ForegroundColorSpan span = new ForegroundColorSpan(Color.BLUE);
        spanString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanString;
    }
    //设置字体加粗
    public static  CharSequence addBoldSpan(CharSequence charSequence,int start ,int end) {
        SpannableString spanString = new SpannableString(charSequence);
        StyleSpan span = new StyleSpan(Typeface.BOLD_ITALIC);//加粗
        spanString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanString;
    }

    //给字体中间添加删除线
    public static  CharSequence addStrikeSpan(CharSequence charSequence,int start ,int end) {
        SpannableString spanString = new SpannableString(charSequence);
        StrikethroughSpan span = new StrikethroughSpan();
        spanString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanString;
    }

    //给字体添加下划线
    public static  CharSequence addUnderLineSpan(CharSequence charSequence,int start ,int end) {
        SpannableString spanString = new SpannableString(charSequence);
        UnderlineSpan span = new UnderlineSpan();
        spanString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanString;
    }
    //设置是字体大小
    public static CharSequence addSizeSpan(CharSequence charSequence,int start ,int end,int size) {
        SpannableString spannableString = new SpannableString(charSequence);
        AbsoluteSizeSpan sizeSpan01 = new AbsoluteSizeSpan(size, true);
        spannableString.setSpan(sizeSpan01, start, start, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }
}

